package com.cloud.xtools.user.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.cloud.xtools.user.domain.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Daixinguo
 */
@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    @Select("SELECT * FROM roles ${ew.customSqlSegment}")
    @Results({
            // 这里 id 需要显式映射一下才行，自动映射有时可能会因为某些配置或特殊情况而失效
            @Result(property = "id", column = "id"),
            // 关联权限
            @Result(property = "permissions",
                    javaType = List.class,
                    // 指定用于关联查询的列，这里是 User 实体的 id 列
                    column = "id",
                    many = @Many(select = "selectPermissionsByRoleId"))
    })
    List<Role> roleList(@Param(Constants.WRAPPER) Wrapper<Role> wrapper);

    // 查询权限
    @Select("SELECT * FROM permissions WHERE id IN (SELECT permission_id FROM role_permissions WHERE role_id = #{roleId})")
    List<Role> selectPermissionsByRoleId(Long roleId);
}
